package com.ruoyi.web.controller.server;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.EmWarning;
import com.ruoyi.system.domain.dto.CallUserDto;
import com.ruoyi.system.domain.dto.WarningUserDto;
import com.ruoyi.system.service.IEmWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 预警信息Controller
 *
 * @author yangcan
 * @date 2023-03-07
 */
@RestController
@RequestMapping("/system/warning")
public class EmWarningController extends BaseController
{
    @Autowired
    private IEmWarningService emWarningService;

    /**
     * 查询预警信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EmWarning emWarning)
    {
        startPage();
        List<EmWarning> list = emWarningService.selectEmWarningList(emWarning);
        return getDataTable(list);
    }

    /**
     * 导出预警信息列表
     */
    @Log(title = "预警信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmWarning emWarning)
    {
        List<EmWarning> list = emWarningService.selectEmWarningList(emWarning);
        ExcelUtil<EmWarning> util = new ExcelUtil<EmWarning>(EmWarning.class);
        util.exportExcel(response, list, "预警信息数据");
    }

    /**
     * 获取预警信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(emWarningService.selectEmWarningById(id));
    }

    /**
     * 新增预警信息
     */
    @Log(title = "预警信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmWarning emWarning)
    {
        return toAjax(emWarningService.insertEmWarning(emWarning));
    }

    /**
     * 修改预警信息
     */
    @PreAuthorize("@ss.hasPermi('system:warning:edit')")
    @Log(title = "预警信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmWarning emWarning)
    {
        return toAjax(emWarningService.updateEmWarning(emWarning));
    }

    /**
     * 删除预警信息
     */
    @Log(title = "预警信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(emWarningService.deleteEmWarningByIds(ids));
    }

    @PostMapping("/pullUserInfo")
    public AjaxResult pullUserInfo(@RequestBody WarningUserDto dto) throws Exception {
        emWarningService.pullUserInfo(dto) ;
        return success() ;
    }

    @GetMapping("/getWarningUserInfo")
    public AjaxResult getWarningUserInfo(@RequestParam("id") Long id) throws Exception {
        List<SysUser> list = emWarningService.getWarningUserInfo(id) ;

        return success(list) ;
    }

    @PostMapping("/callUser")
    public AjaxResult callUser(@RequestBody CallUserDto dto) throws Exception {
        emWarningService.callUser(dto) ;
        return success() ;
    }

    /**
     * 查询预警信息列表
     */
    @GetMapping("/getMyWarning")
    public TableDataInfo getMyWarning(EmWarning emWarning)
    {
        startPage();
        emWarning.setUserId(SecurityUtils.getUserId());
        emWarning.setIsHandle(1L);
        List<EmWarning> list = emWarningService.getMyWarning(emWarning);
        return getDataTable(list);
    }

    @PutMapping("/commitHandleResult")
    public AjaxResult commitHandleResult(@RequestBody EmWarning emWarning)
    {
        if (!(!Objects.isNull(emWarning.getHurt()) && !Objects.isNull(emWarning.getDeath()) && StringUtils.isNotBlank(emWarning.getTreatment()) && StringUtils.isNotBlank(emWarning.getWastage()))){
            throw new ServiceException("上报处理信息不能为空!") ;
        }
        emWarning.setIsHandle(2L);
        return toAjax(emWarningService.updateEmWarning(emWarning));
    }

    @GetMapping("/warningAllWorker")
    public AjaxResult warningAllWorker(@RequestParam("id") Long id){
        emWarningService.warningAllWorker(id) ;
        return success() ;
    }
}
